/*=======================================================================================
	Robustness.do
	
		Uses the Personal Finance Website (PFW) data to analyze the response of 
		consumption to the APFD payments across different subpopulations
		
	Author: Lorenz Kueng, June 2015
=========================================================================================*/

cap log close PFW_09
log using "$homedir/log-files/PFW_09_$date.log", text replace name(PFW_09)


*** Load data

use "$homedir/data/stata/PFW_quarterly.dta", clear 

	generate date = Quarter
	format   date %tq
	
	lab var APFD "PFD payments" // APFD payments indentified via direct deposit entry in early October

	xtset userid date

	
*** Create additional variables

	* controls
	foreach var in residentialstatus marriagestatus {
		display _n(1) "`var'"
		cap drop `var'_
		encode   `var', generate(`var'_)
		bysort userid (date): egen _temp = mode(`var'_), maxmode
		replace  `var'_ = _temp if `var'_==. & _temp!=. 
		replace  `var'_ = 999   if `var'_==. 
		drop _temp
	}
	foreach var in age edu profession homezipcode {
		display _n(1) "`var'"
		cap drop `var'_
		generate `var'_ = `var'
		bysort userid (date): egen _temp = mode(`var'_), maxmode
		replace  `var'_ = _temp if `var'_==. & _temp!=. 
		replace  `var'_ = 999   if `var'_==. 
		drop _temp
	}
	
	cap drop _temp
	gen D_incomeQ = D.incomeQ
	bysort userid (Quarter): egen _temp = mean(D_incomeQ)
	replace D_incomeQ = _temp if missing(D_incomeQ)==1
	drop _temp*
	
	foreach var in incomeQ D_incomeQ incomeY liquid_asset1 {
		winsor `var', g(`var'W) p(0.01)
		sum `var' `var'W, d
	}
	
	** total expenditures as a measure of permanent income (excluding uncategorized transactions, ie only nondurables, services, durables)
	cap drop totexp1_annual
	bysort userid: egen totexp1_annual = mean(totexp1) 
	replace totexp1_annual =  totexp1_annual*4
	replace totexp1_annual = -totexp1_annual
	lab var totexp1_annual "annualized total expenditures (totexp1)"
	summarize totexp1_annual, detail
	winsor    totexp1_annual, p(0.01) generate(totexp1_annualW)
	summarize totexp1_annual totexp1_annualW, detail
	
	
	
*================================	
*** Run regressions
*===============================



*========================================================
* Deductions, voluntary and involuntary. Voluntary and involuntary deductions instituted by the Alaska Permanent Fund Dividend Division (APFDD)
*========================================================

	cap log close DEDUCT
	log using "$homedir/results/tables/PFW_11_Deductions_voluntary_and_involuntary_${date}.log", text replace name(DEDUCT)

		generate frac_voluntary   = APFD_deduc_voluntary  /(APFD_AllPayments+APFD_deduc_voluntary)
		generate frac_involuntary = APFD_deduc_involuntary/(APFD_AllPayments+APFD_deduc_involuntary)

		foreach var in frac_voluntary frac_involuntary {
			display _n(5)"`var'"
			summarize `var' if APFDid_annual!=., detail
			summarize `var' if APFDid_annual!=. & `var'!=0, detail
			summarize `var', detail
			summarize `var' if `var'!=0, detail
		}
		
	cap log close DEDUCT


*========================================================
* Robustness of excess sensitivity results
*========================================================

global file "$homedir/results/tables/TableA4_ExcessSensitivity_Robustness_${date}"
cap rm "${file}.xls"
cap rm "${file}.txt"

			
	global depvar = "nondur_serv"


	*-----------------------------------------------------------
	* Winsorize dependent variable at 1%
	*-----------------------------------------------------------

	xtset userid date

	* make expenditures positive

	replace ${depvar} = -${depvar}

	* save a copy of the original variable

	cap drop original
	generate original = ${depvar}

	* winsorize at 1% (conditional on non-zero observations for disaggregated categories)

	summarize ${depvar}, detail
	summarize ${depvar} if ${depvar}!=0, detail

	count if ${depvar}==0 

	if `r(N)'<5000 {

		cap drop temp
		winsor ${depvar}, generate(temp) p(0.01) 
		replace temp = 0 if temp==.
		replace ${depvar} = temp
	}
	else { // winsorize conditional on positive entries (non-zeros) for disaggregated variables
		
		capture noisily {
		cap drop temp
		winsor ${depvar} if ${depvar}!=0, generate(temp) p(0.01) 
		replace temp = 0 if temp==.
		replace ${depvar} = temp
		}
	}	
	summarize ${depvar}, detail
	summarize ${depvar} if ${depvar}!=0, detail
	cap drop temp

	* make all variables positive (i.e., expenditures)
		
	replace $depvar = abs(${depvar}) 
	summarize ${depvar}, detail

	xtset userid date

	


	*-----------------------------------------------------------
	* Regressions
	*-----------------------------------------------------------
	
	*** 1. Baseline from previous table (to make sure we have the same spec)

		generate _APFD = APFD // make copy of original APFD
		replace  APFD = APFD_DirectDep_start_Oct
	
		reghdfe D.${depvar} APFD /// 
				liquid_asset1 D_incomeQ incomeY /// liquidit constraints: low liquid asssets (liquid_asset1), current income 'shocks' (D_incomeQ), and accumulated income shocks (incomeY)
				totexp1_annual /// being poor (low permanent income)
			if APFDid_annual!=. ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)

				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("baseline") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
					) ///
					keep( APFD )

				
	***2. Use all dividends, also those received as checks in addition to late direct deposits

		replace  APFD = APFD_AllPayments
		
		reghdfe D.${depvar} APFD ///
				liquid_asset1 D_incomeQ incomeY /// 
				totexp1_annual /// 
			if APFDid_AllPayments_annual!=. ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)

			outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label  addstat(Number of clusters,e(N_clust)) ///
				ctitle("all dividends") ///
				addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
				) ///
				keep( APFD )
					
			
	***3. Condition on receiving only a partial dividend

		generate _temp = (APFDid_Donated_annual!=.) + (APFDid_Garnished_annual!=.)
		replace  _temp =. if _temp==0 & state=="AK" 

		reghdfe D.${depvar} APFD ///
				liquid_asset1 D_incomeQ incomeY /// 
				totexp1_annual /// 
			if APFDid_annual!=. & _temp!=. ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)

			outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label  addstat(Number of clusters,e(N_clust)) ///
				ctitle("partial dividend") ///
				addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
				) ///
				keep( APFD )
				
		drop _temp


	***4. Condition on receiving full dividend

		reghdfe D.${depvar} APFD ///
				liquid_asset1 D_incomeQ incomeY /// 
				totexp1_annual /// 
			if APFDid_annual!=. & APFDid_FullAmount_annual!=. & (NumbChecks==APFDid_FullAmount_annual|state=="WA") ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)
			
			outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label  addstat(Number of clusters,e(N_clust)) ///
				ctitle("full dividend") ///
				addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
				) ///
				keep( APFD )
				
			
	***4b. Use all direct deposits, also late ones

		replace  APFD = APFD_DirectDep
		
		reghdfe D.${depvar} APFD ///
				liquid_asset1 D_incomeQ incomeY /// 
				totexp1_annual /// 
			if APFDid_DirectDep_annual!=. ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)

			outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label  addstat(Number of clusters,e(N_clust)) ///
				ctitle("all direct deposits") ///
				addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
				) ///
				keep( APFD )

				
	***5. Include also Alaskans for whom I don't see a dividend

		replace  APFD = APFD_AllPayments
		
		reghdfe D.${depvar} APFD ///
				liquid_asset1 D_incomeQ incomeY /// 
				totexp1_annual /// 
			/// if APFDid_AllPayments_annual!=. /// 
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)

			outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label  addstat(Number of clusters,e(N_clust)) ///
				ctitle("all Alaskans") ///
				addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
				) ///
				keep( APFD )



	***6. Sample of userser with self-reported family size equals the number of checks received => robustness to user vs household level

		count if (family_size==NumbChecks|state=="WA") & APFDid_annual!=. & liquid_asset1!=.

		reghdfe D.${depvar} APFD ///
				liquid_asset1 D_incomeQ incomeY /// 
				totexp1_annual /// 
			if APFDid_annual!=. & (family_size==NumbChecks|state=="WA") ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)
			
			outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label  addstat(Number of clusters,e(N_clust)) ///
				ctitle("NubCheck=family_size") ///
				addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
				) ///
				keep( APFD )
		
				

	***7. log-changes instead of level differences (as in Hsieh 2003 => requires normalizing PFD by income or total expenditures)
	
		**Add PFD to regular income
		
		generate  _APFD_annual = APFD_annual
		replace   _APFD_annual = 0 if _APFD_annual==.
		
		generate  incomeY_incl_PFD = incomeYW + _APFD_annual
		summarize incomeY_incl_PFD, detail
		count if  incomeY_incl_PFD==0
		tabmiss   incomeY_incl_PFD

		
		**Dividend per average quarterly household income as in Hsieh (2003)
		
		bysort userid (date): egen _temp = mean(incomeY_incl_PFD) // average annual income
		//generate APFD_inc = APFD_AllPayments/(_temp/4) // Hsieh's specification: dividend divided by average quarterly household income
		generate APFD_inc = APFD_DirectDep_start_Oct/(_temp/4) // Hsieh's specification: dividend divided by average quarterly household income
		drop _temp
		
		
		**Generate log-changes and winsorize 

		generate  ln_${depvar} = log(${depvar}) // log(original+1)
		generate  Dln_${depvar} = D.ln_${depvar}
		summarize Dln_${depvar}, detail
		winsor    Dln_${depvar}, generate(Dln_${depvar}W) p(0.05) // need to winsorize at 5% to keep changes roughly within +/-100%

		
		**Regression
		
		lab var  APFD_inc "PFD payments / family income"

		reghdfe Dln_${depvar}W APFD_inc ///
				liquid_asset1 D_incomeQ incomeY /// 
				totexp1_annual /// 
			if APFDid_annual!=. ///
			, absorb(date Alaska family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)
			// , absorb(date Alaska marriagestatus_ residentialstatus_ age_ edu_ profession_ homezipcode_) vce(cluster userid)
			
			outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label  addstat(Number of clusters,e(N_clust)) ///
				ctitle("log-changes") ///
				addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", Yes, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
				) ///
				keep( APFD_inc ) ///
				sortvar( APFD APFD_inc )

				

		***8. Alaskan's only

		replace APFD = _APFD		
		
		reghdfe D.${depvar} APFD ///
				liquid_asset1 D_incomeQ incomeY ///
				totexp1_annual /// absorbed by HH FEs
			if APFDid_annual!=. & Alaska==1 ///
			, absorb(date family_sizeImp age_ edu_ marriagestatus_ residentialstatus_ profession_ homezipcode_) vce(cluster userid)			
			
				outreg2 using "${file}.xls",  alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(3) se nocons label addstat(Number of clusters, e(N_clust)) ///
					ctitle("Alaskans only") ///
					addtext( ///
							" - Family size FE", Yes, ///
							" - Time FE", Yes, /// 
							" - State FE", --, /// 
							" - Liquid assets", Yes, ///
							" - Current income - level and change", Yes, ///
							" - Permanent income", Yes, ///
							" - Household characteristics", Yes ///
					) ///
					keep( APFD )
				
cap rm "${file}.txt"

log close PFW_09
